package com.lingnan.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.lingnan.common.utils.ResultEntity;
import com.lingnan.service.VideoSerivce;
import com.lingnan.servicebase.excetioncontrooller.LingNanException;
import com.lingnan.utils.ConstantPropertiesUtil;
import com.lingnan.utils.InitObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 视频点播实现类
 *
 * @author zzd
 */
@Service
public class VideoServiceImpl implements VideoSerivce {
    /**
     * 视频上传功能
     *
     * @param file
     * @return
     */
    @Override
    public String uploading(MultipartFile file) {

        try {
            // 上传文件原始名称
            String fileName = file.getOriginalFilename();
            // 上传后显示的文件文件名称(去掉最后一个.后面字符串)
            String title = fileName.substring(0, fileName.lastIndexOf("."));
            // 上传文件输入流
            InputStream inputStream = file.getInputStream();
            // 上传文件后返回的文件ID
            String videoId = null;

            UploadStreamRequest request = new UploadStreamRequest(
                    // AccessKey ID
                    ConstantPropertiesUtil.KEY_ID,
                    // AccessKey Secret
                    ConstantPropertiesUtil.KEY_SECRET,
                    // 上传后文件名称
                    title,
                    // 上传文件名称
                    fileName,
                    // 上传文件输入流
                    inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

            System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID

            if (response.isSuccess()) {

//                System.out.print("VideoId=" + response.getVideoId() + "\n");

                videoId = response.getVideoId();
            } else {
                //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
//                System.out.print("VideoId=" + response.getVideoId() + "\n");
//                System.out.print("ErrorCode=" + response.getCode() + "\n");
//                System.out.print("ErrorMessage=" + response.getMessage() + "\n");
                videoId = response.getVideoId();
            }

            // 返回上传后的视频ID
            return videoId;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 批量删除视频
     *
     * @param videoSourceIdList
     */
    @Override
    public void deleteAllVideo(List<String> videoSourceIdList) {
        try {
            // 创建初始化对象
            DefaultAcsClient client = InitObject.initVodClient(ConstantPropertiesUtil.KEY_ID, ConstantPropertiesUtil.KEY_SECRET);
            // 创建request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 设置需要删除的视频id
            String videoSourceIds = StringUtils.join(videoSourceIdList.toArray(), ",");
            request.setVideoIds(videoSourceIds);
            // 使用初始化对象执行
            client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new LingNanException(20001,"删除课程视频失败");
        }

    }
}
